package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dto.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    @Override
    public ResponseResult findByNameAndPage(ChannelDTO channelDTO) {
        //TODO 参数检测
        if (channelDTO==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //TODO 分页检测
        channelDTO.checkParam();
        //TODO 分页参数检查
        IPage<AdChannel> pageRe = new Page<>(channelDTO.getPage(), channelDTO.getSize());
        //todo 根据名称模糊查询
        LambdaQueryWrapper<AdChannel> lambdaQuery = Wrappers.lambdaQuery();//构建条件查询器
        if (StringUtils.isNotBlank(channelDTO.getName())){
            lambdaQuery.like(AdChannel::getName,channelDTO.getName());
        }
        //todo 排序
        lambdaQuery.orderByAsc(AdChannel::getOrd);
        //分页参数
        IPage<AdChannel> pageResult = this.page(pageRe, lambdaQuery);
        //TODO 封装结果
        PageResponseResult responseResult =
                new PageResponseResult(channelDTO.getPage(), channelDTO.getSize(), pageResult.getTotal());

        responseResult.setData(pageResult.getRecords());

        return responseResult;
    }

    /*
    TODO 频道新增
    *  */
    @Override
    public ResponseResult addAdChannel(AdChannel adChannel) {
        //TODO 参数校验 名字不为空   长度小于10    名字不能重复
        String name = adChannel.getName();
        if (StringUtils.isBlank(name)||name.length()>10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名字不能为空，名字长度不能大于10");
        }
        //todo 查询名称是否重复
        LambdaQueryWrapper<AdChannel> lambdaQuery = Wrappers.lambdaQuery();
        LambdaQueryWrapper<AdChannel> eq = lambdaQuery.eq(AdChannel::getName, name);
        int count = this.count(eq);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道的名称已存在");
        }
        //todo 保存频道
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        return ResponseResult.okResult();
    }

    /*
    * TODO 频道修改
    *  */
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {

        //TODO  参数校验
        //todo 检查id是否为空
        if (adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"id 不能为空");
        }
        //todo 根据id检查修改以前的数据   频道名称 不为空 并且和之前频道名称不一样   + 判断是否重复的判断
        AdChannel byId = getById(adChannel.getId());
        if (byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"未发现对应频道的信息");
        }
        String name = adChannel.getName();
        if (StringUtils.isNotBlank(name)&&!name.equals(byId.getName())){
            LambdaQueryWrapper<AdChannel> lambdaQuery = Wrappers.lambdaQuery();
            LambdaQueryWrapper<AdChannel> eq = lambdaQuery.eq(AdChannel::getName, name);
            int count = this.count(eq);
            if (count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道名称已存在，请换一个名称");
            }
        }
        this.updateById(adChannel);
        return ResponseResult.okResult();
    }

    /*
    * TODO 频道删除
    *  */
    @Override
    public ResponseResult deleteById(Integer id) {
        //todo 检查参数
        //todo 检查id是否为空
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"id 不能为空");
        }
        //todo 根据id检查数据
        AdChannel adChannel = getById(id);
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"数据不存在");
        }
        //todo 检查频道状态是否微true,启用状态为true,禁用状态为false
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE);
        }
        this.deleteById(id);
        return ResponseResult.okResult();
    }
}
